﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reader
{
    class DBHelper
    {
        //数据库连接字符串
        public static string ConnString = "server=.;database=BooksDB;uid=sa;pwd=wang0118";
        //数据库连接对象
        public static SqlConnection Conn = null;
        //初始化数据库连接
        public static void lnitConnection()
        {
            //如果连接对象不存在，则创建连接
            if (Conn == null)
            {
                Conn = new SqlConnection(ConnString);
            }
            //如果连接数据库对象关闭。则打开链接
            if (Conn.State == ConnectionState.Closed)
            {
                Conn.Open();
            }
            //如果连接中断。则重新连接
            if (Conn.State == ConnectionState.Broken)
            {
                Conn.Close();
                Conn.Open();
            }
        }

        //查询，获取DataReader
        public static SqlDataReader GetSqlDataReader(string sqlStr)
        {
            lnitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            //CommandBehavior.CloseConnection命令行为，当DataReader对象被关闭时，自动关闭占用的连接对象
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        //查询，获取DataTable
        public static DataTable GetDataTable(string sqlStr)
        {
            lnitConnection();
            DataTable table = new DataTable();
            SqlDataAdapter dap = new SqlDataAdapter(sqlStr, Conn);
            dap.Fill(table);
            Conn.Close();
            return table;

        }
        //增、删、改操作
        public static bool ExecuteNonQuery(string sqlStr)
        {
            lnitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            int result = cmd.ExecuteNonQuery();
            Conn.Close();
            return result > 0;
        }
        //执行集合函数
        public static object ExecuteScalar(string sqlStr)
        {
            lnitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            object result = cmd.ExecuteScalar();
            Conn.Close();
            return result;
        }
    }
}
